<?php

namespace app\common\model\xilujob;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;
use function fast\array_get;

class ResumePersonalInfo extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'xilujob_resume_personal_info';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'work_experience',
        'education_level',
        'current_state',
        'age',
        'gender_text',
        'marital_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }



    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function workExperience() {
        return $this->belongsTo('WorkExperience','work_experience_id','id',[],'LEFT');
    }

    public function educationLevel() {
        return $this->belongsTo('EducationLevel','education_level_id','id', [], 'LEFT');
    }

    public function currentState() {
        return $this->belongsTo('WorkState','current_state_id','id',[],'LEFT');
    }

    public function getGenderTextAttr($value, $data) {
        $value = $value ? : array_get($data,'gender');
        switch ($value) {
            case 1:
                return '男';
            case 2:
                return '女';
            default:
                return '';
        }
    }

    public function getMaritalTextAttr($value, $data) {
        switch($this->marital) {
            case 1:
                return '已婚';
            case 2:
                return '未婚';
            default:
                return '';
        }
    }

    public function getAgeAttr($value, $data) {
        $birthdate = array_get($data, 'birthdate');
        if(!$birthdate)
            return '';
        $age = floor((strtotime(date('Y-m-d')) - strtotime($birthdate))/86400/365);
        return $age;
    }

}
